package com.ruoyi.system.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.PerformVo;
import com.ruoyi.system.domain.vo.StatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GxPerformanceMapper;
import com.ruoyi.system.domain.GxPerformance;
import com.ruoyi.system.service.IGxPerformanceService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 绩效Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-15
 */
@Service
public class GxPerformanceServiceImpl implements IGxPerformanceService
{
    @Autowired
    private GxPerformanceMapper gxPerformanceMapper;

    /**
     * 查询绩效
     *
     * @param performanceId 绩效主键
     * @return 绩效
     */
    @Override
    public GxPerformance selectGxPerformanceByPerformanceId(Long performanceId)
    {
        return gxPerformanceMapper.selectGxPerformanceByPerformanceId(performanceId);
    }

    /**
     * 查询绩效列表
     *
     * @param gxPerformance 绩效
     * @return 绩效
     */
    @Override
    public List<GxPerformance> selectGxPerformanceList(GxPerformance gxPerformance)
    {
        return gxPerformanceMapper.selectGxPerformanceList(gxPerformance);
    }

    public List<StatisticsVo> selectGxPerformanceByUserAndDateRange(PerformVo performVo)
    {
        String startTime = performVo.getStartTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (startTime == null) {
            return gxPerformanceMapper.selectGxPerformanceByUserAndDateRange(performVo);
        }
        Date start = null;
        try {
            start = simpleDateFormat.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String dateKey = performVo.getDateKey();


        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        if (dateKey.equals("week")) {
            calendar.add(Calendar.WEEK_OF_YEAR,1);
        } else if (dateKey.equals("month")) {
            calendar.add(Calendar.MONTH,1);
        } else {
            calendar.add(Calendar.YEAR,1);
        }
        Date endTime = calendar.getTime();
        String end = simpleDateFormat.format(endTime);
        performVo.setEndTime(end);
        return gxPerformanceMapper.selectGxPerformanceByUserAndDateRange(performVo);
    }

    /**
     * 新增绩效
     *
     * @param gxPerformance 绩效
     * @return 结果
     */
    @Override
    public int insertGxPerformance(GxPerformance gxPerformance)
    {
        gxPerformance.setCreateTime(DateUtils.getNowDate());
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        gxPerformance.setUserId(user.getUserId());
        return gxPerformanceMapper.insertGxPerformance(gxPerformance);
    }

    /**
     * 修改绩效
     *
     * @param gxPerformance 绩效
     * @return 结果
     */
    @Override
    public int updateGxPerformance(GxPerformance gxPerformance)
    {
        gxPerformance.setUpdateTime(DateUtils.getNowDate());
        return gxPerformanceMapper.updateGxPerformance(gxPerformance);
    }

    /**
     * 批量删除绩效
     *
     * @param performanceIds 需要删除的绩效主键
     * @return 结果
     */
    @Override
    public int deleteGxPerformanceByPerformanceIds(Long[] performanceIds)
    {
        return gxPerformanceMapper.deleteGxPerformanceByPerformanceIds(performanceIds);
    }

    /**
     * 删除绩效信息
     *
     * @param performanceId 绩效主键
     * @return 结果
     */
    @Override
    public int deleteGxPerformanceByPerformanceId(Long performanceId)
    {
        return gxPerformanceMapper.deleteGxPerformanceByPerformanceId(performanceId);
    }
}
